import { vector3Offset, convertColorFormat } from "./common.js";
//绘制柱状拉伸体
export function extrudeEntity({
  points,
  height,
  color,
  opacity = 1,
  castShadow = false,
  objectName,
  tag,
  baseHeight,
}) {
  let scene = window.viewer.scene;
  //水平cart3集合
  let outer = new Li.Cartesian3Vector();
  points = normalizeHeight(points, baseHeight);
  points.forEach((item) => {
    outer.push_back(item.toCartesian3());
  });

  //拉伸体对象
  let extrudeEntity = new Li.ExtrudeEntity();
  // extrudeEntity.pivot = Li.Pivot.MODELTOP;
  extrudeEntity.setOuter(outer);
  extrudeEntity.extrudeHeight = height;
  //着色
  color = convertColorFormat(color);
  extrudeEntity.color = Li.Color.fromRgb(color.r, color.g, color.b, 255);
  extrudeEntity.alpha = opacity;
  let altitude = points[0].toCartographic().height;
  extrudeEntity.altitude = altitude;
  extrudeEntity.create();
  let entity = extrudeEntity.createEntity();
  entity.travalRenderers(function (renderer) {
    if (castShadow == true) {
      renderer.castShadow = true;
    }
    renderer.material.opacity = opacity;
  });

  if (objectName) {
    entity.objectName = objectName;
  }
  if (tag != undefined) {
    entity.tag = tag + "";
  }
  //加入场景
  scene.addEntity(entity);
  outer.delete();
  extrudeEntity.delete();
  return entity;
}
//所有点统一高度
export function normalizeHeight(pointArray, type = "bottom") {
  let heightList = [];
  pointArray.forEach((item) => {
    item = Li.Vector3.create(item.x, item.y, item.z);
    let degPoint = item.toCartographic().toDegrees();
    heightList.push(degPoint.height);
  });
  let minHeight = Math.min(...heightList);
  let maxHeight = Math.max(...heightList);
  let midHeight = (minHeight + maxHeight) / 2;
  pointArray = pointArray.map((item) => {
    item = Li.Vector3.create(item.x, item.y, item.z);
    let degPoint = item.toCartographic();
    switch (type) {
      case "bottom":
        degPoint.height = minHeight;
        break;
      case "middle":
        degPoint.height = midHeight;
        break;
      case "top":
        degPoint.height = maxHeight;
        break;
      default:
        break;
    }
    return degPoint.toVector3();
  });
  return pointArray;
}
// 创建线条
export function createPolyline3d(lineWidth, color) {
  let polyline = new Li.Polyline3D();
  polyline.setWidth(lineWidth || 2.5);
  polyline.color = color || Li.Color.fromRgb(252, 20, 20, 255);
  polyline.setAltitudeMethod(Li.AltitudeMethod.Absolute);
  return polyline;
}
// 创建面
export function createPolygon3d({
  points,
  name = "",
  lineWidth = 1, //线宽
  lineColor = "#fff", //线条颜色
  lineAlpha = 1, //线条透明度
  fillColor = "#fff", //填充颜色
  fillAlpha = 0.8, //填充透明度
  // mode = Li.AltitudeMethod.Absolute,
}) {
  let polygon3d = new Li.Polygon3D();
  points.forEach((point) => {
    point = Li.Vector3.create(point.x, point.y, point.z);
    polygon3d.addPoint(point);
  });
  polygon3d.setWidth(lineWidth); //线宽
  let rgb = convertColorFormat(lineColor);
  lineColor = Li.Color.fromRgb(rgb.r, rgb.g, rgb.b, 255); //Math.ceil(lineOpacity *255)
  polygon3d.color = lineColor; //线条颜色
  polygon3d.alpha = lineAlpha; //线条透明度
  //
  let fillRgb = convertColorFormat(fillColor);
  fillColor = Li.Color.fromRgb(fillRgb.r, fillRgb.g, fillRgb.b, 255);
  polygon3d.setFillColor(fillColor); //填充颜色
  polygon3d.fillAlpha = fillAlpha; //填充透明度
  // polygon3d.setAltitudeMethod(mode);

  polygon3d.draw();
  polygon3d.end();
  polygon3d.objectName = name;
  return polygon3d;
  // let polygon3d = new Li.Polygon3D();
  // polygon3d.color = Li.Color.fromRgb(0, 0, 0, 128);
  // polygon3d.alpha = 0.5;
  // polygon3d.fillAlpha = 0.6;
  // polygon3d.setFillColor(Li.Color.fromRgb(255, 255, 255, 56));
  // // polygon3d.setAltitudeMethod(Li.AltitudeMethod.Absolute);
  // return polygon3d;
}
// 创建标签
export function createLabel3d(
  {
    name,
    position,
    url,
    text,
    imageWidth,
    imageHeight,
    uid,
    fontSize,
    color,
    background,
  },
  billboardCollection
) {
  let label = new Li.Label3D();
  label.name = name;
  label.position = position;
  label.text = text;
  label.fontSize = fontSize || 11;
  label.fontColor = color || Li.Color.fromRgb(0, 0, 0, 255);
  label.background = background || Li.Color.fromRgb(255, 255, 255, 255);
  if (url) {
    label.url = url;
    label.imageWidth = imageWidth;
    label.imageHeight = imageHeight;
  }
  if (uid) {
    label.addProperty("uid", uid);
  }
  if (name == "兴趣点") {
    label.textHorizontal = Li.Horizontal.RIGHT;
    label.textVertical = Li.Vertical.CENTER_V;
    label.addProperty("tagName", "兴趣点");
  }
  label.setCollection(billboardCollection);
  return label;
}
// 创建billboard物体
export function createBillboardEntity(
  { name, point, url, size },
  billboardCollection
) {
  let bbEntity = new Li.BillboardEntity();
  bbEntity.position = point;
  bbEntity.setAltitudeMethod(SSmap.AltitudeMethod.OnBuilding);
  bbEntity.url = url;
  // bbEntity.vertical = Li.Vertical.CENTER_V;
  bbEntity.imageWidth = size || 50;
  bbEntity.imageHeight = size || 50;
  if (name) {
    bbEntity.objectName = name;
  }
  bbEntity.setCollection(billboardCollection);

  return bbEntity;
}
//创建正方形
export function createSquare({
  centerPoint,
  name = "square",
  side = 0.8, //边长
  lineWidth = 1, //线宽
  lineColor = "#fff", //线条颜色
  lineAlpha = 1, //线条透明度
  fillColor = "#fff", //填充颜色
  fillAlpha = 0.8, //填充透明度
  index = "",
  method = Li.AltitudeMethod.Absolute,
}) {
  let polygon3d = new Li.Polygon3D();
  let radius = Number(side) / 2;
  let range = [
    [-radius, radius],
    [radius, radius],
    [radius, -radius],
    [-radius, -radius],
  ];
  range.forEach((r) => {
    let toPoint = vector3Offset(centerPoint, {
      offsetX: r[0],
      offsetY: r[1],
    });
    polygon3d.addPoint(toPoint);
  });
  polygon3d.setWidth(lineWidth); //线宽
  let rgb = convertColorFormat(lineColor);
  lineColor = Li.Color.fromRgb(rgb.r, rgb.g, rgb.b, 255); //Math.ceil(lineOpacity *255)
  polygon3d.color = lineColor; //线条颜色
  polygon3d.alpha = lineAlpha; //线条透明度
  //
  let fillRgb = convertColorFormat(fillColor);
  fillColor = Li.Color.fromRgb(fillRgb.r, fillRgb.g, fillRgb.b, 255);
  polygon3d.setFillColor(fillColor); //填充颜色
  polygon3d.fillAlpha = fillAlpha; //填充透明度
  polygon3d.setAltitudeMethod(method);
  polygon3d.draw();
  polygon3d.end();
  polygon3d.objectName = name;
  polygon3d.tag = index + "";
  return polygon3d;
}
// 模型
export function createModel({ url, uid, name }) {
  let scene = window.viewer.scene;
  let model = new Li.Model(url);
  model.tag = uid;
  model.objectName = name;
  let entity = new Li.Entity();
  entity.addComponent(model);
  entity.tag = uid;
  entity.objectName = "model";
  scene.addEntity(entity);
  // model.srs = "EPSG:4547";
  return model;
}
// 坡度分析
export function createSlopeAndAspect(pointArray) {
  let slopeAndAspect = new Li.SlopeAndAspect();
  for (var i = 0; i < pointArray.length; i++) {
    slopeAndAspect.add(pointArray[i]);
  }
  slopeAndAspect.create();
  slopeAndAspect.name = "slopeAndAspect";
  return slopeAndAspect;
}
// 场景特效
export function createParticle(lonlat, options) {
  // emitter
  var emitter = new Li.ParticleEmitter();
  emitter.shapeType = options.shapeType;
  emitter.shapeSize = options.shapeSize;
  emitter.fillShape = options.fillShape;
  emitter.size = options.size;
  emitter.endSize = options.endSize;
  emitter.sizeVariation = options.sizeVariation;
  emitter.emitRate = options.emitRate;
  emitter.lifeSpan = options.lifeSpan;

  // velocity
  var vec = new Li.ParticlePointDirection();
  vec.x = options.velocity[0];
  vec.y = options.velocity[1];
  vec.z = options.velocity[2];
  vec.xVariation = options.velocityVariation[0];
  vec.yVariation = options.velocityVariation[1];
  vec.zVariation = options.velocityVariation[2];
  emitter.velocity = vec;

  // acceleration
  var acc = new Li.ParticlePointDirection();
  acc.x = options.acceleration[0];
  acc.y = options.acceleration[1];
  acc.z = options.acceleration[2];
  acc.xVariation = options.accelerationVariation[0];
  acc.yVariation = options.accelerationVariation[1];
  acc.zVariation = options.accelerationVariation[2];
  emitter.acceleration = acc;

  // renderer
  var renderer = new Li.ImageParticleRenderer();
  renderer.color = options.color;
  renderer.colorVariation = options.colorVariation;
  renderer.alpha = options.alpha;
  renderer.alphaVariation = options.alphaVariation;
  renderer.alignedAxis = options.alignedAxis;
  renderer.blendMode = options.blendMode;
  // renderer.image = this.$baseUrl + options.image;
  renderer.image = options.image;

  // particle
  var particleSystem = new Li.ParticleSystem();
  particleSystem.addEmitter(emitter);
  particleSystem.addRenderer(renderer);

  // entity
  var entity = new Li.Entity();
  entity.transform.cartographic = lonlat;
  entity.addComponent(particleSystem);
  window.viewer.scene.addEntity(entity);

  // return particleSystem;
  return entity;
}
//创建裁切体
export function createClipVolume(opt) {
  let scene = window.viewer.scene;
  let clipVolume = new Li.ClipVolume();
  clipVolume.shape = Li.ClipVolumeShape.Cube; //剪切形状, 默认立方体
  clipVolume.clipFlag = Li.ClipVolumeFlag.ClipScene; //默认是全部剪切, 可选ClipGlobe, ClipScene, ClipAll
  clipVolume.size = opt.size; // 裁切体大小
  clipVolume.showDebugVolume = false; //是否显示剪切的轮廓
  clipVolume.objectName = "clipVolume"; //添加一个名称标识
  var entity = new Li.Entity();
  entity.transform.cartographic = opt.position; //坐标点
  var childEntity = new Li.Entity(); //子物体
  childEntity.parent = entity; //设置父子结构
  childEntity.transform.rotation = opt.rotation; //旋转角度(四元数)
  childEntity.addComponent(clipVolume); //添加组件
  // entity.addComponent(clipVolume);
  scene.addEntity(entity);
  return {
    clipVolume,
    entity,
    childEntity,
  };
}
//创建裁切面
export function createCilpSurface(opt) {
  //客户端instanceof只能判断js类型，无法判断底层类型
  let scene = window.viewer.scene;
  let outer = new Li.Cartesian3Vector();
  opt.pointArray.forEach((item) => {
    //笛卡尔坐标添加
    // if (item instanceof Li.Vector3) {
    //     outer.push_back(item.toCartesian3());
    // } else if (item instanceof Li.Cartesian3) {
    outer.push_back(item);
    // }
  });
  let extrudeEntity = new Li.ExtrudeEntity();
  extrudeEntity.setOuter(outer);
  extrudeEntity.extrudeHeight = opt.extrudeHeight || -0.1;
  extrudeEntity.color = Li.Color.fromRgb(255, 0, 0, 255);
  extrudeEntity.alpha = 0.2; //透明度
  extrudeEntity.create();
  let ent = extrudeEntity.createEntity();
  ent.enabled = opt.show;
  scene.addEntity(ent);
  extrudeEntity.delete();
  outer.delete();
  return ent;
}
